
package smtp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;
import java.io.IOException;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMessage;

import com.sun.mail.imap.IMAPFolder;
/**
 * Leader if this Email client
 * controller for the main screen
 * setup UI and sub-controller
 * listen to all the button events and select on change events
 * @author liweihan
 *
 */
public class MainScreenController {
	Session session=null;
	Store store = null;
	MainScreen ui=null;
	MailBoxController box=null; 
	MessagesListViewer msgList =null;
	ReceivedEmailParser current=null;
	String user=null;

/**
 * setup UI and attributes
 * @param session user session
 * @param store java connection store
 */
	public MainScreenController(Session session, Store store) {
		user=session.getProperties().getProperty("user");
		ui= new MainScreen(this);
		ui.setUpUI();
		this.session=session;
		this.store=store;
		box = new MailBoxController(store, ui.getBoxList());
		box.updateJList(ui.getBoxList());
		//box.addFocusListener(new BoxFocusListener());
		msgList= new MessagesListViewer((IMAPFolder) box.getInbox());
		
		box.addFolderChangeListener(msgList.getChangeFolderListener());
		
		ui.setMessageListPanel(msgList);

		//box.addFocusListener(new BoxFocusListener());

		ui.validate();


		msgList.addMessageSelectedListener(new MessageOnChangeListener());
		current= new ReceivedEmailParser(user);
		ui.setVisible(true);

	}
	/**
	 * handle the event when user click on the message list (middle one)
	 * update current email and call UI to update right side message panel
	 * @author liweihan
	 *
	 */
	protected class MessageOnChangeListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			current.setMimeMessage((MimeMessage)(e.getSource()));
			ui.updateMessage(current);
		}

	}
	/**
	 * handle the event when user select a new message
	 * update current email
	 * NO LONGER USE
	 * @author liweihan
	 *
	 */
	protected class MessageFocusListener implements FocusListener {
		@Override
		public void focusGained(FocusEvent e) {
			current.setMimeMessage((MimeMessage)(e.getSource()));
			System.out.println("new Email selected");

		}
		@Override
		public void focusLost(FocusEvent e) {
			// TODO Auto-generated method stub

		}
	}

	/**
	 * handle mail box folder list onFcus event
	 * NO LONGER USE
	 * @author liweihan
	 *
	 */
	protected class BoxFocusListener implements FocusListener {

		@Override
		public void focusGained(FocusEvent e) {

			//folder.changeFolder((IMAPFolder)e.getSource());
		}

		@Override
		public void focusLost(FocusEvent arg0) {
			// TODO Auto-generated method stub

		}
	}
	/**
	 * handle button events, open new Message panel for writing a new or reply a message
	 * @author liweihan
	 *
	 */
	protected class ButtonListener implements ActionListener {
		@Override

		public void actionPerformed(ActionEvent e) {
			String com=e.getActionCommand();
			if(com.equals("Compose")) {
				new MessageScreen(null, session, false);
			} else if(com.equals("Reply")) {
				reply(false);
			} else if(com.equals("Reply All")){
				reply(true);
			}else if (com.equals("Forward")){
				new MessageScreen(current,session,true);
			}else if(com.startsWith("Download")){
				if(current.getValid()){
					String userDesktop = System.getProperty( "user.home" ) + File.separator + "Desktop"+File.separator;
					System.out.println(userDesktop);
					try {
						if(current.saveAttch(userDesktop)){
							ui.popUpError("Saved to "+userDesktop);							
						}
					} catch (IOException e1) {
						ui.popUpError("Invalid file address, IO Exception");
					} catch (MessagingException e1) {
						ui.popUpError("Can't donwload attachement, MessagingException");
					}					
				}
			}
		}
	}
	/**
	 * help to construct the reply message panel
	 * @param toAll true if replay to all
	 */
	private void reply(boolean toAll){
		ReceivedEmailParser reply=new ReceivedEmailParser(user);
		if(!current.getValid()){
			ui.popUpError("No email is currently selected");
		}else{
			reply.setMimeMessage((MimeMessage) current.getReply(toAll));
			
			new MessageScreen(reply,session,true);
		}
	}
}








